﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using mshtml;
using System.Runtime.InteropServices;


namespace WpfBrowserTest
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetTopWindow(IntPtr hWnd);

        private HttpMonitorLib.HttpMonClass monitor;

        public MainWindow()
        {
            InitializeComponent();

            wbMain.Navigate("about:blank");
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if(monitor == null)
            {
                monitor = new HttpMonitorLib.HttpMonClass();
                //monitor.IEWindow = GetTopWindow(GetTopWindow(GetTopWindow(webBrowser1.Handle))).ToInt32();
                //monitor.IEWindow = GetTopWindow(GetTopWindow(GetTopWindow(wbMain.Handle))).ToInt32();
                monitor.IEWindow = GetTopWindow(GetTopWindow(wbMain.Handle)).ToInt32();
                monitor.OnRequest += new HttpMonitorLib._IHttpMonEvents_OnRequestEventHandler(monitor_OnRequest);
                monitor.OnRedirect += new HttpMonitorLib._IHttpMonEvents_OnRedirectEventHandler(monitor_OnRedirect);
                monitor.OnDataRecieved += new HttpMonitorLib._IHttpMonEvents_OnDataRecievedEventHandler(monitor_OnDataRecieved);
                monitor.OnResponse += new HttpMonitorLib._IHttpMonEvents_OnResponseEventHandler(monitor_OnResponse);
            }

            wbMain.Navigate("http://www.facebook.de");

            //String strRestult = wbMain.Document.ToString();
            //HTMLDocument mainDoc = (HTMLDocument)wbMain.Document;

            //dynamic TST = mainDoc.childNodes;
            
            //IHTMLElement ELE = mainDoc.documentElement;
            //String strTest = ELE.innerHTML;

            //foreach(IHTMLElement E in mainDoc.scripts)
            //{
            //    HTMLScriptElement S = E as HTMLScriptElement;

            //    if(S.innerHTML != null &&
            //       S.innerHTML.ToLower().Contains("var seed"))
            //    {
            //        this.Title = "FOUND SEED!!!!!! HOOOOORRRRRAY";
            //    }
            //}
        }

        Dictionary<int, String> _dicRequestURLs = new Dictionary<int, String>();
        Dictionary<int, String> _dicReceivedData = new Dictionary<int, String>();

        private void monitor_OnRequest(int id, string url, string headers, string method, object postData)
        {
            if(url.ToLower().Contains("main_src.php"))
            {
                String strTest = ByteArrayToString((Byte[])postData);
                if(strTest != "")
                {
                }
            }

            if(!_dicRequestURLs.ContainsKey(id))
            {
                _dicRequestURLs.Add(id, url);
                textBox1.Text = String.Concat(
                    textBox1.Text,
                    "Request: ",
                    url,                    
                    Environment.NewLine);
            }
        }

        private void monitor_OnRedirect(int id, int redirectedId, string url, string redirectedUrl, string responseHeaders, string requestHeaders)
        {
        }

        private void monitor_OnDataRecieved(int id, string url, object data, int status)
        {
            String strTest = ByteArrayToString((Byte[])data);
            if(strTest != "")
            {
            }

            if(((Byte[])data).Length > 0)
            {
                if(!_dicReceivedData.ContainsKey(id))
                {
                    _dicReceivedData.Add(id, ByteArrayToString((Byte[])data));
                }
                else
                {
                    _dicReceivedData[id] = String.Concat(
                        _dicReceivedData[id],
                        ByteArrayToString((Byte[])data));
                }
            }

            foreach(String strSeek in _dicReceivedData.Values)
            {
                if(strSeek.ToLower().Contains("var seed"))
                {
                }
            }
        }

        private void monitor_OnResponse(int id, string url, int responseCode, string headers)
        {
        }

        private byte[] StringToByteArray(string str)
        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            return enc.GetBytes(str);
        }

        private string ByteArrayToString(byte[] arr)
        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            return enc.GetString(arr);
        }
    }
}
